package Day01_Reflect;

import TestClass.Person;

import java.lang.reflect.Field;

public class Demo02_Reflect {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//        1.获取Person的Class对象
        Class personClass = Person.class;
//        2.获取成员变量们,Field[]获取所有public修饰的成员变量
        Field[] fields = personClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
        Field num = personClass.getField("num");
        System.out.println("-----------");
        //          3.获取成员变量num的值
        Person p = new Person();
        Object o = num.get(p);
        System.out.println(o);
//            4.设置成员变量num的值
            num.set(p,5);
        System.out.println(p);
        System.out.println("-----------");

        // getDeclaredFlieds 获取所有的不管什么修饰符修饰的成员变量
        Field[] f3 = personClass.getDeclaredFields();
        for (Field f4 : f3) {
            System.out.println(f4);
        }


    }
}
